import * as config from './config.js'

export function uploadFile(filePath) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${config.baseUrl}/upload/fileParam`,
			filePath: filePath,
			name: 'uploadFile',
			success(result) {
				const data = JSON.parse(result.data)
				if (result.statusCode !== 200) {
					console.log('上传失败', JSON.stringify(result));
					reject(data)
					return
				}
				console.log('上传成功', JSON.stringify(result));
				resolve(data)
			},
			fail(error) {
				console.log('上传失败', JSON.stringify(error));
				reject(error)
			},
			complete() {
				console.log('上传完成');
			}
		})
	})
}

export function uploadImage() {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			success(res) {
				uploadFile(res.tempFilePaths[0])
					.then(result => {
						console.log(result);
						resolve(result)
					}).catch(error => {
						reject(error)
						uni.showToast({
							icon: 'none',
							title: `上传失败${JSON.stringify(error)}`
						})
					})
			}
		})
	})
}


export function uploadIdCardFile(filePath) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${config.baseUrl}/upload/fileIdNo`,
			filePath: filePath,
			name: 'uploadFile',
			success(result) {
				const data = JSON.parse(result.data)
				if (result.statusCode !== 200) {
					console.log('上传失败', JSON.stringify(result));
					reject(data)
					return
				}
				console.log('上传成功', JSON.stringify(result));
				resolve(data)
			},
			fail(error) {
				console.log('上传失败', JSON.stringify(error));
				reject(error)
			},
			complete() {
				console.log('上传完成');
			}
		})
	})
}

export function saveFileMoreParam(data){
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			success(res) {
				uni.uploadFile({
					url: `${config.baseUrl}/upload/saveFileMoreParam`,
					filePath: res.tempFilePaths[0],
					formData:{
						...data
					},
					name: 'uploadFile',
					success(result) {
						const data = JSON.parse(result.data)
						if (result.statusCode !== 200) {
							console.log('上传失败', JSON.stringify(result));
							reject(data)
							return
						}
						console.log('上传成功', JSON.stringify(result));
						resolve(data)
					},
					fail(error) {
						console.log('上传失败', JSON.stringify(error));
						reject(error)
					},
					complete() {
						console.log('上传完成');
					}
				})
			}
		})
	})
}